import com.adobe.cairngorm.control.CairngormEventDispatcher;

import mx.events.CloseEvent;
import mx.events.ValidationResultEvent;
import mx.managers.PopUpManager;

import net.optix.ip.events.UserLoginEvent;
import net.optix.ip.model.ModelLocator;
import net.optix.ip.vo.UserVO;

[Bindable]
public var model:ModelLocator	=	ModelLocator.getInstance(); 
  
public function	init():void
{
	model.bSubmit	=	true;
 	model.result	=	"";
  	model.close		=	"no";
}

private function titleWindow_close(evt:CloseEvent):void 
{  	
	model.result	=	"";
  	model.bSubmit	=	true;
  	try
	{
		PopUpManager.removePopUp(this); 
	}
	catch(error:Error){}
}
            
public function login():void
{
	var result:Boolean	=	true;

	var resultValidate:ValidationResultEvent;
				
	resultEmail.text		=	"";
	resultPassword.text		=	"";
	model.result			=	"";
				
	resultValidate	=	emailValidator.validate(email.text);
 	if(resultValidate.type == ValidationResultEvent.INVALID)
	{
     	result	=	false;
     	resultEmail.text	=	
     	resultValidate.message;
	}
            	
	stringValidator.minLength	=	6;
  	resultValidate	=	stringValidator.validate(password.text);
  	if(resultValidate.type == ValidationResultEvent.INVALID)
   	{
     	result	=	false;
     	resultPassword.text	=	
            			resultValidate.message;
 	}
            	
	model.bSubmit	=	false;
	model.result	=	"";
            	
  	if(result == true)
  	{
     	var vo:UserVO	=	new	UserVO();
     	vo.email	=	email.text;
       	vo.password	=	password.text;
            		
     	var event:UserLoginEvent	=	new UserLoginEvent(vo);
	   	CairngormEventDispatcher.getInstance().dispatchEvent( event );
   	}
	else
 	{
      	model.bSubmit	=	true;
  	}
}